/*********************************************************************************
 *      Copyright:  (C)Guo Zixuan
 *                  All rights reserved.
 *
 *       Filename:  client_socket.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(09/04/25)
 *         Author:  Guo Zixuan <guozixuan@gmail.com>
 *      ChangeLog:  1, Release initial version on "09/04/25 20:15:41"
 *                 
 ********************************************************************************/
#include "server_socket.h"

int socket_server_init(char *listen_ip, int listen_port)
{
	struct sockaddr_in      servaddr;
	int                     rv = 0;
	int                     on = 1;
	int                     listenfd;

	if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		log_error("Use socket() to creat a TCP socket failure: %s\n", strerror(errno));
		return -1;
	}
	                                                                                                                              
	setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

	memset(&servaddr, 0, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(listen_port);

	if(!listen_ip)       //没传ip
	{
		servaddr.sin_addr.s_addr = htonl(INADDR_ANY);      //监听所有ip    
	}
	else
	{
		if(inet_pton(AF_INET, listen_ip, &servaddr.sin_addr) <= 0)     //将ip转为4字节存入sin_addr     
		{
			log_error("inet_pton() set listen IP address failure.\n");
			rv = -2;
			goto CleanUp;
		}
	}

	if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
	{
		log_error("Use bind() to bind the TCP socket failure: %s\n", strerror(errno));
		rv = -3;
		goto CleanUp;
	}

	if(listen(listenfd, 13) < 0)
	{
		log_error("Use bind() to bind the TCP socket failure: %s\n", strerror(errno));
		rv = -4;
		goto CleanUp;
	}

CleanUp:
	if(rv<0)
		close(listenfd);
	else
		rv = listenfd;

	return rv;
}

void set_socket_rlimit(void)
{
	struct rlimit limit = {0};

	getrlimit(RLIMIT_NOFILE, &limit);
	limit.rlim_cur = limit.rlim_max;
	setrlimit(RLIMIT_NOFILE, &limit);

	log_warning("set socket open fd max count to %d\n", limit.rlim_max);
}
